package com.wmx.likou;

import java.util.Stack;

public class SingleLinkedList {
    private Node head;

    class Node{
        private Object data;
        private Node next;
        Node(Object data){
            this.data = data;
        }
    }

    //从头部添加 每次添加头部都在变化
    public void addRear(Object data){
        Node newNode = new Node(data);
        Node previous  = null;
        Node current = head;
        if(previous == null){
            head = newNode;
        }
       newNode.next = current;
    }



    //添加无序链表(从尾部添加) 每次添加头部没有变化
    public void addHead(Object data){
        Node newNode = new Node(data);
        Node cure = head;
        if(head == null){
            head =newNode;
        }else{
            while (true){
                if(cure.next == null){
                    cure.next = newNode;
                    break;
                }
                cure =  cure.next;

            }
        }
    }





    //添加有序链表
    public void addOrderBy(Object data){
        Node newLink = new Node(data);
        Node previous = null;
        Node current = head;
        while(current != null && (int)data > (int)current.data){ //找下个节点 找到要插入节点的位置
            previous = current;
            current = current.next;
        }
        //比当前节点值小，刚好插入当前节点前面
        if(previous == null){//链表是空的
            head = newLink;
        }
        else{
            previous.next = newLink;
        }
        newLink.next = current;
    }
    //获得头部
    public Node getHead(){
        return  head ;
    }

    public void display(Node head){
        System.out.printf("[");
        while (true){
            if(head.next == null){
                System.out.printf(head.data+"]");
                break;
            }

            System.out.printf(head.data+"->");
            head = head.next;
        }

    }


    public Node removeElements(Node head, int val) {
        Stack<Node> stack = new Stack();
        if(head == null ) {
            return head;
        }
        if(head.next == null && (int)head.data ==val){
            return null;
        }

        while(true){

            if((int)head.data != val){
                stack.push(head);
            }
            if(head.next == null){
                break ;
            }

            head = head.next;

        }
        Node first = null;
        while(!stack.isEmpty()){
             Node temp = stack.pop();
            if(first == null){

                first =temp;
                first.next = null;

            }else{
                temp.next = first;
                first = temp;

            }
        }


        return first;
    }

    public static void main(String[] args) {
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addHead(1);
        singleLinkedList.addHead(2);
        singleLinkedList.addHead(6);
        singleLinkedList.addHead(3);
        singleLinkedList.addHead(4);
        singleLinkedList.addHead(5);
        singleLinkedList.addHead(6);

        singleLinkedList.display(singleLinkedList.getHead());
        singleLinkedList.removeElements(singleLinkedList.getHead(),6);
        singleLinkedList.display(singleLinkedList.getHead());





    }


}
